package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.domain.User;
import com.tanhua.dubbo.mappers.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

//service接口实现类
@DubboService   //服务提供者暴露接口
public class UserApiImpl extends ServiceImpl<UserMapper,User> implements UserApi{
    @Autowired
    private UserMapper userMapper;

    /**
     * 通过用户手机号查询用户
     * @param mobile
     * @return
     */
    @Override
    public User findByMobile(String mobile) {
        //使用LambdaQueryWrapper查询用户
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper();
        wrapper.eq(User::getMobile,mobile);
        User user = userMapper.selectOne(wrapper);

        //返回用户
        return user;
    }

    /**
     * 将用户存入数据库表，并返回用户id
     * @param user
     * @return
     */
    @Override
    public Long keep(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    /**
     * 为新用户更新环信账号字段
     * @param user
     */
    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    /**
     * 通过id查询用户信息
     * @param userId
     * @return
     */
    @Override
    public User findById(Long userId) {
        return userMapper.selectById(userId);
    }

    /**
     * 通过环信id查询用户
     * @param huanxinId
     * @return
     */
    @Override
    public User findByHXId(String huanxinId) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getHxUser,huanxinId);
        return userMapper.selectOne(wrapper);
    }
}
